import 'package:flutter/material.dart';
import '../services/ScreenAdatper.dart';
import '../widget/jdText.dart';
import '../widget/jdButton.dart';
import 'package:dio/dio.dart';
import 'package:fluttertoast/fluttertoast.dart';
import '../config/config.dart';
import 'dart:convert';
import '../pages/tabs/tab.dart';
import '../services/Storage.dart';
class LoginPage extends StatefulWidget {
  LoginPage({Key key}) : super(key: key);

  _LoginPageState createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  String _userName;
  String _passWord;

  login() async{
    var api = "${Config.domain}api/doLogin";
      var reslut = await Dio().post(api,data: {
        "username":this._userName, 
        "password":this._passWord
      }); 
      print(reslut);
      if(reslut.data['success']){  
        Fluttertoast.showToast(
          msg: "${reslut.data['message']}",
          toastLength: Toast.LENGTH_SHORT,
          gravity: ToastGravity.CENTER
        );
        Storage.setString('userinfo',json.encode(reslut.data['userinfo']));

        // 这个相当于跳回跟页面  new Tabs 就是页面  记住还要把 import '../tabs.dart'; 页面引进来
        Navigator.of(context).pushAndRemoveUntil(
          new MaterialPageRoute(builder: (context) => new Tabs()),
          (route) => route == null
        );
      }else{
        Fluttertoast.showToast(
          msg: "${reslut.data['message']}",
          toastLength: Toast.LENGTH_SHORT,
          gravity: ToastGravity.CENTER
        );
      }
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: IconButton(
          icon: Icon(Icons.close),
          onPressed: (){
            Navigator.of(context).pop();
          },
        ),
        title: Text("登陆"),
        actions: <Widget>[
          FlatButton(
            child: Text("客服"),
          )
        ],
      ),
      body: ListView(
        padding: EdgeInsets.fromLTRB(20, 0, 20, 0),
        children: <Widget>[
          Center(
            child:Container(
              margin: EdgeInsets.only(top: 50),
              height: ScreenAdatper.height(160),
              width: ScreenAdatper.width(160),
              child: Image.network("https://xcxt.qzriji.com/public/webImages/logo.png"),
            ),
          ),
          SizedBox(height: 50,),
          JdText(
            text: '请输入用户名',
            onChanged: (value){
              setState(() {
               this._userName = value; 
              });
            },
          ),
          SizedBox(height: 20,),
          JdText(
            password: true,
            text: '请输入密码',
            onChanged: (value){
             setState(() {
               this._passWord = value; 
              });
            },
          ),
          SizedBox(height: 20,),
          Container(
            padding: EdgeInsets.only(bottom: 20),
            child:Stack(
              children: <Widget>[
                Align(
                  alignment: Alignment.centerLeft,
                  child: Text("忘记密码"),
                ),
                Align(
                  alignment: Alignment.centerRight,
                  child: InkWell(
                    onTap: (){
                      Navigator.pushNamed(context, '/register1');
                    },
                    child: Text("用户注册"),
                  ),
                )
              ],
            ) ,
          ),
          JdButtoon(
            color: Colors.red,
            text: '登陆',
            onTap: this.login,
          )
        ],
      ),
    );
  }
}